// owner-app/pages/my-dwell-detailsthei/index.js
import { handlerGobackClick, onlyChangeRouter } from "../../../utils/util";
const app = getApp();
import Apipublic from "../../../models/api";
const apipublic = new Apipublic();
import User from "../../../models/user";
const user = new User();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: "",
    timea: new Date().getTime(),
    myDwellInfo: {},
    // 拍摄
    photo: "",
    pictureFlag: false,
    cameraShow: false,
    devicePosition: "front",
    photoShow: false, //拍摄是否成功  拍摄：成功
    didNot: true,
    options: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      options,
    });
    this.initPageData(options);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var { baseImgUrl } = getApp().globalImgUrl.config;
    this.setData({
      baseImgUrl,
      timea: new Date().getTime(),
    });
  },
  handlerGobackClick,
  cancelPicture() {
    this.initPageData(this.data.options);
    this.setData({
      cameraShow: false,
    });
  },
  //重新拍摄
  toShoot() {
    this.setData({
      photo: "",
      cameraShow: true,
    });
  },
  //拍摄
  shootHandle() {
    let that = this;
    that.setData({
      photo: "",
      photoShow: false,
      pictureFlag: true,
      didNot: true,
    });

    wx.getSetting({
      success(res) {
        if (res.authSetting["scope.camera"]) {
          that.setData({
            cameraShow: true,
          });
          that.contextPhoto();
        } else {
          // 授权
          wx.authorize({
            scope: "scope.camera",
            success() {
              that.setData({
                cameraShow: true,
              });
              that.contextPhoto();
            },
            fail(fa) {
              that.shootOpen();
            },
          });
        }
      },
    });
  },
  shootOpen() {
    let that = this;
    wx.openSetting({
      success(res) {
        if (res.authSetting["scope.camera"]) {
          that.setData({
            cameraShow: true,
          });
          that.contextPhoto();
        }
      },
    });
  },
  contextPhoto() {
    let that = this;
    const ctx = wx.createCameraContext();
    ctx.takePhoto({
      quality: "high",
      success: (res) => {
        that.setData({
          cameraShow: false,
          photo: res.tempImagePath,
        });

        that.miniImage();
      },
    });
  },
  // 将微信返回的临时路径转为base64
  miniImage() {
    let _this = this;
    wx.getFileSystemManager().readFile({
      filePath: _this.data.photo,
      encoding: "base64",
      success(res) {
        let data = {
          faceBase64: "data:image/jpeg;base64," + res.data,
          commId: wx.getStorageSync("commid"),
          idCard: _this.data.myDwellInfo.idCard,
          name: _this.data.myDwellInfo.name,
        };

        let signBool = true;

        let header = {
          // "Content-Type": "multipart/form-data",   //不要删，后台暂时不需要
          "X-Access-Token": wx.getStorageSync("token"),
        };

        apipublic.userCheckPic(data, header).then((res) => {
          if (res.code == 1000) {
            signBool = false;
            _this.setData({
              photoShow: true,
              didNot: false,
              facePath: res.result,
            });
          }
        });

        if (signBool) {
          _this.setData({
            photoShow: false,
            didNot: true,
          });
        }
      },
    });
  },
  // 摄像头转换
  cameraConversion() {
    console.log("SDK付款了");
    if (this.data.devicePosition == "front") {
      this.setData({
        devicePosition: "back",
      });
    } else {
      this.setData({
        devicePosition: "front",
      });
    }
  },
  error(e) {
    console.log(e.detail, "错了");
  },
  submitLastAll() {
    wx.navigateBack();
    // this.initPageData(this.data.options);
  },
  initPageData(option) {
    user
      .houseHoldDetail(option)
      .then((res) => {
        if (res.code == 1000) {
          this.setData({
            myDwellInfo: res.result,
            photo: res.result.facePath + "?time=" + this.data.timea,
            pictureFlag: res.result.facePath ? false : true,
          });
        }
      })
      .catch((err) => {
        console.log(err);
      });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      cameraShow: false,
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
